package com.boot.controller;


import com.boot.entity.R;
import com.boot.entity.Stats;
import com.boot.entity.Item;
import com.boot.entity.R;
import com.boot.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单详情表(Item)表控制层
 *
 * @author makejava
 * @since 2023-07-20 18:48:25
 */
@RestController
@RequestMapping("/item")
@CrossOrigin
public class ItemController {
    /**
     * 服务对象
     */
    @Autowired
    private ItemService itemService;

    /**
     * 创建新的订单详情
     * @param item
     * @return
     */
    public R insert(Item item) {
        itemService.save(item);
        return R.success("订单详情创建成功！");
    }

    /**
     * 统计销量前十的商品
     * @return
     */
    @RequestMapping("/queryItemAndProductSales")
    public R queryItemAndProductSales(){
        List<Stats> list = itemService.queryItemAndProductSales();
        List<String> s = new ArrayList<>();
        List<BigDecimal> b = new ArrayList<>();
        for (int i =0 ;i<list.size();i++){
            s.add(list.get(i).getStatsName());
            b.add(list.get(i).getSum());
        }
        List l = new ArrayList();
        l.add(s);
        l.add(b);
        return R.success(l);
    }
}

